//
//  JXLEmoticonCollectionViewCell.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/10/8.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit

class JXLEmoticonCollectionViewCell: UICollectionViewCell {
    
    //纪录当前的表情按钮
    private var emoticonButtonArray: [JXLEmoticonButton] = [JXLEmoticonButton]()
    
    //  添加删除表情按钮
    fileprivate lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(deleteButtonAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        return button
    }()

    
    //准备cell需要的数据源
    var emoticonsArray: [JXLEmoticon]? {
        didSet {
            guard let etnArray = emoticonsArray else {
                return
            }
            
            //代码执行到此，表示数据不为空
            
            //把表情按钮全部隐藏，解决cell复用的问题
            for button in emoticonButtonArray {
                button.isHidden = true
            }
            
            //遍历表情数组模型，给表情按钮绑定数据
            for (i, emoticon) in etnArray.enumerated() {
                
                //拿到对应角标的按钮
                let button = emoticonButtonArray[i]
                //设置表情模型
                button.emoticon = emoticon
//                button.isHidden = false
//                
//                if emoticon.type == "0" {
//                    //  表示图片,直接设置button的image,
//                    //  问题-> 图片的路径需要拼接完整
//                button.setImage(UIImage(named:emoticon.path!), for: .normal)
//                    
//                    button.setTitle(nil, for: .normal)
//                } else {
//                    //  表示emoji, 直接设置文本
//                    button.setTitle((emoticon.code! as NSString).emoji(), for: .normal)
//                    button.setImage(nil, for: .normal)
//                }
            }
        }
    }
    
    //纪录cell的indexPath
    var indexPath: IndexPath? {
        didSet {
//         messageLabel.text = "当前显示的是第\(indexPath!.section + 1)组第\(indexPath!.item + 1)行"
        }
    }
    
    
//    // messageLabel
//    fileprivate lazy var messageLabel: UILabel = {
//        let label = UILabel(fontSize: 30, textColor: UIColor.red)
//        
//        return label
//    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() -> Void {
        
        //MARK:添加子控件
        //添加表情按钮
        addChildEmoticonButton()
        
        //添加删除按钮
        contentView.addSubview(deleteButton)
//        //添加显示数量的label
//        contentView.addSubview(messageLabel)
//        
//        //MARK: 布局子控件
//        messageLabel.snp_makeConstraints { (make) in
//            
//            make.center.equalTo(contentView)
//            
//        }
    }
    
    //添加表情button
    private func addChildEmoticonButton() -> Void {
        for _ in 0..<20 {
            let button = JXLEmoticonButton()
            button.addTarget(self, action: #selector(emoticonButtonAction(btn:)), for: .touchUpInside)
            
            //设置Bundle字体大小
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            //添加到cell上
            contentView.addSubview(button)
            //添加到数组中
            emoticonButtonArray.append(button)
        }
    }
    
    //  MARK: --    点击删除按钮处理逻辑
    @objc private func deleteButtonAction() {
        
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedDeleteEmoticonNotification), object: nil)
    }
    
    //  MARK: --    点击表情按钮处理逻辑
    @objc private func emoticonButtonAction(btn: JXLEmoticonButton) -> Void {
        
        let emoticon = btn.emoticon
        //发送通知，把按钮对应的模型传出去
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedEmoticonNotification), object: emoticon)
        
        let popView = JXLEmoticonPopView.emoticonPopView()
        
        popView.emoticonButton.emoticon = emoticon
        //获取window
        let window = UIApplication.shared.windows.last!
        
        window.addSubview(popView)
        
        //  btn在collectionView的区域上转成window的区域
        let convertRect = contentView.convert(btn.frame, to: window)
        
        //  获取这个范围的水平的中心点x
        popView.centerX = convertRect.midX
        popView.y = convertRect.maxY - popView.height
        
        //延时0.25秒移除
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25) { 
            popView.removeFromSuperview()
        }
    }

    
    //布局cell上的button
    override func layoutSubviews() {
        
        //  计算每项的宽度
        let itemWidth = width / 7
        let itemHeight = height / 3

        //  循环遍历表情按钮数组，设置位置
        for (i, button) in emoticonButtonArray.enumerated() {
           //设置button大小
            button.size = CGSize(width: itemWidth, height: itemHeight)
           
            //计算列的索引
            let colIndex = i % 7
            //计算行的索引
            let rowIndex = i / 7
            
            //根据行列数设置表情按钮的x，y坐标
            button.x = CGFloat(colIndex) * itemWidth
            button.y = CGFloat(rowIndex) * itemHeight
           
        }
        
        //  设置删除按钮的大小和坐标
        deleteButton.size = CGSize(width: itemWidth, height: itemHeight)
        deleteButton.x = width - itemWidth
        deleteButton.y = height - itemHeight

    }
    
    }
